<?php

namespace app\api\controller;

// use app\api\controller\Tpmecms;
use app\api\controller\Ltder;

/**
 * 客户
 * 
 */
class Clients extends Ltder
{

    protected $db = 'clients';
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // /**
    //  * 测试方法
    //  *
    //  * @ApiTitle    (测试名称)
    //  * @ApiSummary  (测试描述信息)
    //  * @ApiMethod   (POST)
    //  * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
    //  * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
    //  * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
    //  * @ApiParams   (name="name", type="string", required=true, description="用户名")
    //  * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
    //  * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
    //  * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
    //  * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
    //  * @ApiReturn   ({
    //      'code':'1',
    //      'msg':'返回成功'
    //     })
    //  */
    // public function test()
    // {
    //     $this->success('返回成功', $this->request->param());
    // }

    /**
     * 添加我的客户
     * 
     * @param string $name 姓名
     * @param string $wechat 微信号
     * @param string $tel 电话
     * @param string $sex 性别:1=男,2=女,0=未知
     * @param string $house_status  房屋类型:1=租房,2=买房,3=卖房,5=未知
     * @param string $area_size 意向面积大小
     * @param string $area 意向区域
     * @param string $ps 备注
     * @param string $house_type 意向户型
     * @param string $down_pay 首付预算
     * 
     * @return void
     */
    public function add_clients()
    {
       
        $data['tel'] = $this->request->param('tel','');
        $data['uid'] = $this->auth->id;

        $ltder = $this->GetLtdUserData($this->auth->id);

        $data['ltd_id'] = $ltder['ltd_id'];
        $data['admin_id'] = $ltder['admin_id'];

        $res = Db::name('clients')->where($data)->find();
        if($res){
            $this->error('已有该客户的手机号');
        }

        $data['area_size'] = $this->request->param('area_size','');
        $data['name'] = $this->request->param('name','');
        $data['wechat'] = $this->request->param('wechat','');
        

        $data['sex'] = $this->request->param('sex','0');
        $data['house_status'] = $this->request->param('house_status','');
        $data['ps'] = $this->request->param('ps','');
        $data['area'] = $this->request->param('area','');

        $data['down_pay'] = $this->request->param('down_pay','');
        $data['house_type'] = $this->request->param('house_type','');


        

        $data['time'] = time();

        $res = Db::name($this->db)->insert($data);
        if($res){
            $this->success('添加成功');
        }
    }

    /**
     * 获取我的客户列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_clients_list($page=1,$limit=10)
    {
        $list = Db::name($this->db)
                ->where('uid',$this->auth->id)
                ->order('time desc')
                ->page($page,$limit)
                ->select();

        $this->success('ok',$list);
    }

    /**
     * 编辑客户信息
     *
     * @param int $id id
      * @return void
     */
    public function edit_clients($id)
    {
        $params = $this->request->param(); 
        halt($params);
        $data = $this->get_field_arr($params,['name'
,'wechat'
,'tel'
,'sex'
,'house_status'
,'area_size'
,'area'
,'ps'
,'house_type'
,'down_pay']);
                                            
        $res = Db::name($this->db)->where('id',$id)->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除客户
     *
     * @param int $id id
     * @return void
     */
    public function del_clients($id)
    {
        $res = Db::name($this->db)->where('uid',$this->auth->id)->where('id',$id)->find();
        if($res){
            $this->DelClients($id);
            $this->success('删除成功');
        }else{
            $this->error('没找到');
        }
    }

}
